//
//  ViewController.m
//  打字效果
//
//  Created by tianlong on 2018/3/28.
//  Copyright © 2018年 tianlong. All rights reserved.
//

#import "ViewController.h"
#import "WXTimerHelper.h"
#import "WXPKPrinterEffectView.h"


@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextView *textView;
@property (copy, nonatomic) NSString *contentStr;
// 记录当前打印的文字的索引
@property (assign, nonatomic) NSInteger curIndex;
@property (strong, nonatomic) NSThread *thread;
@property (strong, nonatomic) WXTimerHelper *helper;
@property (weak, nonatomic) UILabel *label;
@property (weak, nonatomic) WXPKPrinterEffectView *pkTextView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    /*
    UIImageView *img = [[UIImageView alloc] init];
    //img.image = [UIImage imageNamed:@"yy"];
    img.alpha = 0.6;
    img.frame = self.view.bounds;
    img.contentMode = UIViewContentModeCenter;
    [self.view addSubview:img];
    self.contentStr = @"曾经有一份真诚的爱情摆在我的面前，我没有珍惜，等到失去的时候才追悔莫及，人世间最痛苦的事情莫过于此。如果上天能够给我一个重新来过的机会，我会对那个女孩子说三个字：\"我爱你\"。如果非要给这份爱加上一个期限，我希望是，一万年...";
    self.curIndex = 0;
    self.helper = [WXTimerHelper scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(animationLabel) userInfo:nil repeats:YES];
    self.textView.backgroundColor = [UIColor greenColor];
    */
    self.textView.hidden = YES;
    self.contentStr = @"曾经有一份真诚的爱情摆在我的面前，我没有珍惜，等到失去的时候才追悔莫及，人世间最痛苦的事情莫过于此。如果上天能够给我一个重新来过的机会，我会对那个女孩子说三个字：\"我爱你\"。如果非要给这份爱加上一个期限，我希望是，一万年...";
    
    
    // 计算文字宽度
    WXPKPrinterEffectView *pkV = [[WXPKPrinterEffectView alloc] initWithUsingAnimate:YES interval:0.1];
    pkV.randomSpeed = YES;
    pkV.textColor = [UIColor redColor];
    pkV.font = [UIFont systemFontOfSize:26];
    pkV.text = self.contentStr;
    pkV.frame = CGRectMake(0, 64, self.view.frame.size.width, 150);
    pkV.backgroundColor = [UIColor lightGrayColor];
    [pkV setAnimationCompletion:^{
        NSLog(@"上一个动画执行完毕，执行下一个");
    }];
    [self.view addSubview:pkV];
    self.pkTextView = pkV;
}

- (void)animationLabel
{
    NSInteger loc = self.curIndex + 1;
    if(loc > self.contentStr.length){
        [self.helper stop];
        return;
    }
    int ran = arc4random_uniform(4)+1;
    NSString *sub = [self.contentStr substringWithRange:NSMakeRange(0, loc)];
    self.curIndex++;
    self.textView.text = sub;
    [self.textView scrollRangeToVisible:NSMakeRange(self.textView.text.length, 1)];
    [NSThread sleepForTimeInterval:ran*0.1];
}

- (IBAction)start:(id)sender {
//    [self.helper resume];
    [self.pkTextView resumeTextAnimate];
}

- (IBAction)pause:(id)sender {
//    [self.helper pause];
    [self.pkTextView pauseTextAnimate];
}
- (IBAction)stop:(id)sender {
//    [self.helper stop];
//    self.curIndex = 0;
//    self.textView.text = nil;
    
    [self.pkTextView stopTextAnimate];
    self.pkTextView.text = @"- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event";
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    NSLog(@"%s",__func__);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
